package com.xshang.event.subscriber;

import com.xshang.event.SaveTenantEvent;
import com.xshang.tenant.TenantDsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 新增租户事件订阅
 */
@Slf4j
@Component
public class SaveTenantSubscriber implements ApplicationListener<SaveTenantEvent> {

    @Resource
    private TenantDsServiceImpl tenantDsService;

    /**
     * 监听到新增租户事件
     * @param event the event to respond to
     */
    @Override
    @Async("taskExecutor")
    public void onApplicationEvent(SaveTenantEvent event) {
        String tenantId = event.getTenantId();
        String tenantName = event.getTenantName();

        String threadName = Thread.currentThread().getName();
        log.info("监听到新增租户事件，开始刷新租户源...租户id: {}, 租户名: {}, 执行线程: {}", tenantId, tenantName, threadName);

        tenantDsService.refresh();

    }
}